#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    ListNode* insertionSortList(ListNode* head)
    {
        if (head == nullptr)
        {
            return nullptr;
        }
        ListNode* dummyNode = new ListNode(-1);
        dummyNode->next = head;
        ListNode* lastNode = head;
        ListNode* cur = head->next;
        while (cur != nullptr)
        {
            if (lastNode->val <= cur->val)
            {
                lastNode = lastNode->next;
                cur = cur->next;
            }
            else
            {
                ListNode* prev = dummyNode;
                while (prev->next->val <= cur->val)
                {
                    prev = prev->next;
                }
                lastNode->next = cur->next;
                cur->next = prev->next;
                prev->next = cur;
                cur = lastNode->next;
            }
        }
        return dummyNode->next;
    }
};


